package com.cskaoyan.service.impl;

import com.cskaoyan.bean.PageData;
import com.cskaoyan.bean.UserParameter;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.model.MarketUser;
import com.cskaoyan.model.MarketUserExample;
import com.cskaoyan.service.UserService;
import com.cskaoyan.utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import java.util.Date;
import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public PageData list(UserParameter userParameter) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlsession.getMapper(MarketUserMapper.class);
        MarketUserExample marketUserExample = new MarketUserExample();

        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        //没有被删除的
        criteria.andDeletedEqualTo(false);

        //分页
        Integer page = userParameter.getPage();
        Integer limit = userParameter.getLimit();
        PageHelper.startPage(page, limit);
        //排序
        String sort = userParameter.getSort();
        String order = userParameter.getOrder();
        marketUserExample.setOrderByClause(sort + " " + order);
        //条件
        String username = userParameter.getUsername();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        String mobile = userParameter.getMobile();
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }

        List<MarketUser> marketUsers = mapper.selectByExample(marketUserExample);

        sqlsession.commit();
        sqlsession.close();
        return PageData.list(marketUsers);
    }

    @Override
    public MarketUser detail(Integer id) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlsession.getMapper(MarketUserMapper.class);
        MarketUser marketUser = mapper.selectByPrimaryKey(id);

        sqlsession.commit();
        sqlsession.close();
        return marketUser;
    }
    @Override
    public void update(MarketUser marketUser) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketUserMapper mapper = sqlsession.getMapper(MarketUserMapper.class);

        marketUser.setUpdateTime(new Date());
        mapper.updateByPrimaryKeySelective(marketUser);

        sqlsession.commit();
        sqlsession.close();
    }

}
